Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enable automatic URL linking #19110

Open
wants to merge 11 commits into
base: master
Choose a base branch
from
Open

Conversation

ryzokuken
Copy link
Collaborator

@ryzokuken ryzokuken commented Nov 26, 2024

Automatically detect links in the text content of a file and automatically generate link annotations at the appropriate locations to achieve automatic link detection and hyperlinking.

References:

Please note that this is a WIP PR for soliciting your feedback while I work on polishing things and hopefully optimizing further.

web/pdf_page_view.js Fixed Show fixed Hide fixed
Copy link
Collaborator

@Snuffleupagus Snuffleupagus left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How does this perform, especially in documents that contain a lot of text?

Also, we probably want a new option/preference to be able to disable this functionality.

}

#processLinks() {
return this.pdfPage.getTextContent().then(content => {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We absolutely cannot fetch the textContent twice for each rendered page, since that'll be really inefficient in general.
Besides, it isn't necessary since the textContent is already available once the textLayer has rendered; see

  • this.highlighter?.setTextMapping(textDivs, textContentItemsStr);
  • /**
    * Store two arrays that will map DOM nodes to text they should contain.
    * The arrays should be of equal length and the array element at each index
    * should correspond to the other. e.g.
    * `items[0] = "<span>Item 0</span>" and texts[0] = "Item 0";
    *
    * @param {Array<Node>} divs
    * @param {Array<string>} texts
    */
    setTextMapping(divs, texts) {
    this.textDivs = divs;
    this.textContentItemsStr = texts;
    }

web/pdf_page_view.js Outdated Show resolved Hide resolved
#processLinks() {
return this.pdfPage.getTextContent().then(content => {
const [text, diffs] = normalizedTextContent(content);
const urlRegex = /\b(?:https?:\/\/|mailto:|www.)(?:[[\S--\[]--\p{P}]|\/|[\p{P}--\[]+[[\S--\[]--\p{P}])+/gmv;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also, the regular expression should probably be created just once (and then cached) to avoid re-creating it for every page.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This might no longer be relevant since it's now a static field on the class.

Comment on lines 1247 to 1248
annotationType: 2,
annotationFlags: 4,
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These should use actual constants, rather than hard-coded numbers.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Removed annotationFlags and the only remaining hardcoded number now is annotationType. Ideally I should still not be assigning any of these manually and using a constructor.

web/pdf_page_view.js Outdated Show resolved Hide resolved
web/pdf_page_view.js Outdated Show resolved Hide resolved
web/annotation_layer_builder.js Outdated Show resolved Hide resolved
@ryzokuken ryzokuken force-pushed the autolink-demo branch 2 times, most recently from a7d0024 to f3737b5 Compare December 10, 2024 23:11
web/autolinker.js Dismissed Show dismissed Hide dismissed
@ryzokuken ryzokuken force-pushed the autolink-demo branch 2 times, most recently from cad781f to 93e5417 Compare December 10, 2024 23:16
@ryzokuken
Copy link
Collaborator Author

@Snuffleupagus

Besides, it isn't necessary since the textContent is already available once the textLayer has rendered; see

I was a bit unsure if I understood exactly what you were suggesting but how does this commit look? It "fetches" the textContents from the previous render of the textLayer and makes the processing step sync.

93e5417

@ryzokuken
Copy link
Collaborator Author

@Snuffleupagus nvm my last comment, I figured it out after looking at pdfPageView._textHighlighter.textDivs a couple of times it occurred to me what you were talking about.

https://github.com/mozilla/pdf.js/pull/19110/files#diff-71772f56be799df522c2076ab5fa476253ef15c607af5812536c41696d97cd59R73

@ryzokuken ryzokuken marked this pull request as ready for review December 16, 2024 22:10
if (
annotation.subtype === "Link" &&
annotation.url === link.url &&
Util.intersect(annotation.rect, link.rect) !== null
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In order to avoid some corner case bug, maybe a better way to do would be to compute the ratio area(intersection) / area(annotation.rect) and if it's greater than a threshold then consider that links are very likely the same.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants